/*
 * Copyright (c) 2023 Erik Nordstrøm <erik@nordstroem.no>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

use std::cmp::Ordering;

/// A sorted subset of the words in a wordlist.
/// All words in this subset have the same length, and are sorted.
#[derive(Clone)]
struct WordlistSubset<'a> {
    /// The length of each word in this subset of the wordlist.
    word_len: usize,
    /// The words in this subset of the wordlist.
    pub(crate) words: &'a [WordlistDecodeEntry<'a>],
}

impl<'a> std::fmt::Debug for WordlistSubset<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(
            f,
            "WordlistSubset {{ word_len: {}, words: &{:?} }}",
            self.word_len, self.words
        )
    }
}

#[derive(Clone, Debug, PartialEq)]
struct WordlistDecodeEntry<'a> {
    pub(crate) word: &'a str,
    byte: u8,
}

impl<'a> PartialOrd for WordlistDecodeEntry<'a> {
    /// Word list decode entries are sorted by the length of the word and then by the word itself
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match self.word.len().partial_cmp(&other.word.len())? {
            Ordering::Equal => self.word.partial_cmp(other.word),
            ordering => Some(ordering),
        }
    }
}
